package com.example.controller;

import com.example.common.Result;
import com.example.domain.Comments;
import com.example.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
public class CommentController {

    @Autowired
    private CommentService commentService;

    /*
    根据书籍Id查找关于该书的相关评论
     */
    @GetMapping("/selectCommentById/{bookId}")
    public Result selectCommentById(@PathVariable Long bookId){
        List<Comments> comments = commentService.selectCommentById(bookId);
        return Result.success(comments);
    }

    /*
    根据用户ID查找该书的相关评论，需要的数据涉及到两个表的查询
     */
    @GetMapping("/selectCommentByUserId/{userId}")
    public Result selectCommentByUserId(@PathVariable Long userId){
        List<Map<String, Object>> maps = commentService.selectCommentByUserId(userId);
        return Result.success(maps);
    }

    /*
    根据id删除评论
     */
    @GetMapping("/deleteCommentById/{id}")
    public Result deleteCommentById(@PathVariable Long id){
        commentService.deleteCommentById(id);
        return Result.success("删除成功");
    }

    /*
    添加评论
     */
    @GetMapping("/addComment")
    public Result addComment(@RequestParam Long userId,@RequestParam Long bookId, @RequestParam String content){
        commentService.addComment(userId, bookId, content);
        return Result.success("评论成功");
    }

    /*
    根据书籍ID查找该书的相关评论，需要的数据涉及到两个表的查询
     */
    @GetMapping("/selectCommentByBookId/{bookId}")
    public Result selectCommentByBookId(@PathVariable Long bookId){
        List<Map<String, Object>> maps = commentService.selectCommentByBookId(bookId);
        return Result.success(maps);
    }

    @GetMapping("/delCommentById/{id}")
    public Result delCommentById(@PathVariable Long id){
        commentService.delCommentById(id);
        return Result.success("删除成功");
    }
}
